package com.example.myproject.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;



public class weather extends DockPanel{
		
public void getWeather(){	 
	String url = "http://api.wunderground.com/api/3a87da2ae3ac5a63/conditions/q/55812.json";
	        
	url = URL.encode(url);

	final JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

	jsonp.setCallbackParam("callback");
	jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>() {
	    @Override
	    public void onFailure(final Throwable caught) {
	        Window.alert("JSONP onFailure");
	    }
	    @Override
	    public void onSuccess(JavaScriptObject s) {
	    JSONObject obj = new JSONObject(s);
	    String result = obj.toString();
		  String sAll = result;
		  JSONObject jA = 
		    (JSONObject)JSONParser.parseLenient(sAll);
		  JSONValue jTry = jA.get("current_observation");
		        
		  JSONObject jB =
		    (JSONObject)JSONParser.parseLenient(jTry.toString());
		  JSONValue temp = jB.get("temp_c");
		  JSONValue visibility = jB.get("visibility_km");
		        
		  String sTemp = temp.toString();
		  String sVisibility = visibility.toString();
		  String output = "Temp Celcius: " + sTemp + "\n" + "Visibility Distance: " + sVisibility + " km";
		   
		  Window.alert(output);
		  
	    }
	    });
	}
}


		